{extend name='public/base' /}
{block name='content'}
{include file='public/content_header' /}
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <!-- 表单头部 -->
                <div class="card-header">
                    <div class="btn-group">
                        <a class="btn flat btn-sm btn-default BackButton">
                            <i class="fas fa-arrow-left"></i>
                            返回
                        </a>
                    </div>
                </div>
                <!-- 表单 -->
                <form id="dataForm" class="form-horizontal dataForm" action="" method="post" enctype="multipart/form-data">
                    <!-- 表单字段区域 -->
                    <div class="card-body">
                        <div class="form-group row rowSelect">
    <label for="user_id" class="col-sm-2 col-form-label">用户</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <select name="user_id" id="user_id" class="form-control select2bs4 fieldSelect" data-placeholder="请选择用户">
            <option value=""></option>
            {foreach name='user_list' id='item'}
    <option value="{$item.id}" {if isset($data) && $data.user_id==$item.id}selected{/if}>
        {$item.username}
    </option>
{/foreach}
        </select>
    </div>

    <script>
        $('#user_id').select2({
            theme: 'bootstrap4'
        });
    </script>
</div>
<div class="form-group row rowSelect">
    <label for="shop_cate_id" class="col-sm-2 col-form-label">商户分类</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <select name="shop_cate_id" id="shop_cate_id" class="form-control select2bs4 fieldSelect" data-placeholder="请选择商户分类">
            <option value=""></option>
            {foreach name='shop_cate_list' id='item'}
    <option value="{$item.id}" {if isset($data) && $data.shop_cate_id==$item.id}selected{/if}>
        {$item.name}
    </option>
{/foreach}
        </select>
    </div>

    <script>
        $('#shop_cate_id').select2({
            theme: 'bootstrap4'
        });
    </script>
</div>
<div class="form-group row rowText">
    <label for="name" class="col-sm-2 col-form-label">名称</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="name" name="name" value="{$data.name|default=''}" placeholder="请输入名称" type="text" class="form-control fieldText">
    </div>
</div>
<div class="form-group row rowText">
    <label for="service" class="col-sm-2 col-form-label">服务</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="service" name="service" value="{$data.service|default=''}" placeholder="请输入服务" type="text" class="form-control fieldText">
    </div>
</div>
<div class="form-group row rowText">
    <label for="desc" class="col-sm-2 col-form-label">描述</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="desc" name="desc" value="{$data.desc|default=''}" placeholder="请输入描述" type="text" class="form-control fieldText">
    </div>
</div>
<div class="form-group row rowMultiSelect">
    <label for="tag" class="col-sm-2 col-form-label">标签</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <select name="tag[]" id="tag" data-placeholder="请选择标签" class="form-control fieldMultiSelect" multiple="multiple">
            <option value=""></option>
                     {foreach tag_list as $key=>$value}
        <option value="{$value.id}" {if isset($data) && in_array($value.id,$data.tag)}selected{/if}>{$value.name}</option>
        {/foreach}



        </select>
    </div>

    <script>
        $('#tag').select2();
    </script>
</div>
<div class="form-group row rowText">
    <label for="shopid" class="col-sm-2 col-form-label">商铺ID</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="shopid" name="shopid" value="{$data.shopid|default=''}" placeholder="请输入商铺ID" type="text" class="form-control fieldText">
    </div>
</div>
    <div class="form-group row rowImage">
        <label for="cover" class="col-sm-2 col-form-label">封面</label>
        <div class="col-sm-10 col-md-4 formInputDiv"> 
            <div class="input-group">
                <input id="cover_file" name="cover_file" type="file" class="file-loading " data-initial-preview="{if isset($data)}{$data.cover}{/if}">
                <input name="cover" id="cover" value="{$data.cover|default=''}" hidden placeholder="请上传图片" class="fieldImage">
                <script>
                     initUploadImg('cover');
                </script>            
            </div>
        </div>
    </div>
<div class="form-group row rowMultiImage">
    <label for="images" class="col-sm-2 col-form-label">图片</label>
    <div class="col-sm-10 col-md-8 formInputDiv">
        <div class="input-group">
            <input id="images_file" name="images_file" type="file" class="file-loading " multiple>
            <input name="images" id="images" value="{$data.images|default=''}" hidden placeholder="请上传图片" class="fieldMultiImage">
            <script>
                initUploadMultiImg('images');
            </script>            
        </div>
    </div>
</div><div class="form-group row rowMobile">
    <label for="mobile" class="col-sm-2 col-form-label">联系电话</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="mobile" name="mobile" value="{$data.mobile|default=''}" placeholder="请输入联系电话" type="tel" maxlength="11" class="form-control fieldMobile">
    </div>
</div>
<div class="form-group row rowText">
    <label for="address" class="col-sm-2 col-form-label">地址</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="address" name="address" value="{$data.address|default=''}" placeholder="请输入地址" type="text" class="form-control fieldText">
    </div>
</div>
    
<div class="form-group row rowMap">
    <label class="col-sm-2 col-form-label"></label>
    <div class="col-sm-8 ">
        <div class="input-group mb-3 mapInputContainer">
            <input class="form-control mapKeywords" id="map_key_lng"  placeholder="输入关键字搜索地点" />
        </div>

        <div class="input-group mb-3 mapTipsContainer">
            <span class="form-control mapAddressTips" >
            参考地址：<span id="map_tips_lng" class="mapClipboardSpan" title="点击复制地址" data-toggle="tooltip" data-clipboard-target="#map_tips_lng"></span>
</span>
        </div>
        
        <div id="map_container_lng" class="mapContainer">
        </div>
        <input name="lng" hidden id="lng" value="{$data.lng|default='117'}">
        <input name="lat" hidden id="lat" value="{$data.lat|default='36'}" >
    </div>

    
<script>
    AMapUI.loadUI(['misc/PositionPicker'], function(PositionPicker) {
        		
        let defaultLng = "{$data.lng|default='117'}";
        let defaultLat = "{$data.lat|default='36'}";
        
        let map_lng = new AMap.Map('map_container_lng', {
            zoom: 16,
            scrollWheel: false,
        })
        let positionPicker = new PositionPicker({
            mode: 'dragMap',
            map: map_lng
        });

        positionPicker.on('success', function(positionResult) {
            console.log(positionResult);
            $('#map_tips_lng').html(positionResult.address);
            console.log('success');
            $('#lng').val(positionResult.position.lng);
            $('#lat').val(positionResult.position.lat);
        });      
        positionPicker.on('fail', function(positionResult) {
            console.log(positionResult);
        });
        if(defaultLng!=='0'){
          positionPicker.start(new AMap.LngLat(defaultLng, defaultLat));  
        }else {
            positionPicker.start();
        }
         
        map_lng.panBy(0, 1);
        map_lng.addControl(new AMap.ToolBar({
            liteStyle: true
        }));
        // 输入提示
        let autoOptions = {
            input: "map_key_lng"
        };
        let auto = new AMap.Autocomplete(autoOptions);
        let placeSearch = new AMap.PlaceSearch({
            map: map_lng
        });  
        // 构造地点查询类
        auto.on('select',function (e){
            placeSearch.setCity(e.poi.adcode);
            positionPicker.start(new AMap.LngLat(e.poi.location.lng,e.poi.location.lat ));
        });
    });
     var mapClipboard = new ClipboardJS('.mapClipboardSpan');
     mapClipboard.on('success', function(e) {
         layer.tips('地址复制成功！', '#map_tips_lng', {
             tips: [1,]
         });
         console.info('Action:', e.action);
         console.info('Text:', e.text);
         console.info('Trigger:', e.trigger);
         e.clearSelection();
    });

    mapClipboard.on('error', function(e) {
        console.error('Action:', e.action);
        console.error('Trigger:', e.trigger);
    });

   
</script>
</div>
<div class="form-group row rowText">
    <label for="business_hours" class="col-sm-2 col-form-label">营业时间</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="business_hours" name="business_hours" value="{$data.business_hours|default=''}" placeholder="请输入营业时间" type="text" class="form-control fieldText">
    </div>
</div>
<div class="form-group row rowSwitch">
    <label for="status" class="col-sm-2 col-form-label">状态</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
    <input {if((!isset($data)&&0==1)||(isset($data)&&$data.status==1))}checked{/if}
     class="input-switch"  id="status" value="1" type="checkbox" />
    <input class="switch fieldSwitch" placeholder="状态" name="status" value="{$data.status|default='0'}" hidden />
    </div>

    <script>
        $('#status').bootstrapSwitch({
            onText: "是",
            offText: "否",
            onColor: "success",
            offColor: "danger",
            onSwitchChange: function (event, state) {
                $(event.target).closest('.bootstrap-switch').next().val(state ? '1' : '0').change();
            }
        });
    </script>
</div>
<div class="form-group row rowSwitch">
    <label for="top" class="col-sm-2 col-form-label">是否置顶</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
    <input {if((!isset($data)&&0==1)||(isset($data)&&$data.top==1))}checked{/if}
     class="input-switch"  id="top" value="1" type="checkbox" />
    <input class="switch fieldSwitch" placeholder="是否置顶" name="top" value="{$data.top|default='0'}" hidden />
    </div>

    <script>
        $('#top').bootstrapSwitch({
            onText: "是",
            offText: "否",
            onColor: "success",
            offColor: "danger",
            onSwitchChange: function (event, state) {
                $(event.target).closest('.bootstrap-switch').next().val(state ? '1' : '0').change();
            }
        });
    </script>
</div>
<div class="form-group row rowText">
    <label for="sort" class="col-sm-2 col-form-label">排序</label>
    <div class="col-sm-10 col-md-4 formInputDiv">
        <input id="sort" name="sort" value="{$data.sort|default='0'}" placeholder="请输入排序" type="text" class="form-control fieldText">
    </div>
</div>

                    </div>
                    <!-- 表单底部 -->
                    <div class="card-footer">
                        <div class="row">
                            <div class="col-sm-2">
                            </div>

                            <div class="col-sm-10 col-md-4 formInputDiv">

                                <div class="btn-group">
                                    <button type="submit" class="btn btn-sm btn-primary dataFormSubmit">
                                        保 存
                                    </button>
                                </div>
                                <div class="btn-group">
                                    <button type="reset" class="btn btn-sm btn-default dataFormReset">
                                        重 置
                                    </button>
                                </div>

                                {if !isset($data)}
                                <div class="btn-group float-right createContinueDiv">
                                    <input type="checkbox" class="form-check-input" value="1" id="_create" name="_create" title="继续添加数据">
                                    <label class="form-check-label" for="_create">继续添加</label>
                                </div>
                                {/if}
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
<script>
    /** 表单验证 **/
    $('#dataForm').validate({
        rules: {
                'user_id': {
                required: true,

    },
    'shop_cate_id': {
                required: true,

    },
    'name': {
                required: true,

    },
    'service': {
                required: true,

    },
    'desc': {
                required: true,

    },
    'tag[]': {
                required: true,

    },
    'cover': {
                required: true,

    },
    'images[]': {
                required: true,

    },
    'mobile': {
                required: true,

    },
    'address': {
                required: true,

    },
    'business_hours': {
                required: true,

    },
    'status': {
                required: true,

    },
    'top': {
                required: true,

    },
    'sort': {
                required: true,

    },

        },
        messages: {
                'user_id': {
               required: "用户不能为空",

    },
    'shop_cate_id': {
               required: "商户分类不能为空",

    },
    'name': {
               required: "名称不能为空",

    },
    'service': {
               required: "服务不能为空",

    },
    'desc': {
               required: "描述不能为空",

    },
    'tag[]': {
               required: "标签不能为空",

    },
    'cover': {
               required: "封面不能为空",

    },
    'images[]': {
               required: "图片不能为空",

    },
    'mobile': {
               required: "联系电话不能为空",

    },
    'address': {
               required: "地址不能为空",

    },
    'business_hours': {
               required: "营业时间不能为空",

    },
    'status': {
               required: "状态不能为空",

    },
    'top': {
               required: "是否置顶不能为空",

    },
    'sort': {
               required: "排序不能为空",

    },

        }
    });
</script>
{/block}